package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.apis.wemedia.IRegisterWmUserClient;
import com.heima.model.admin.pojos.ApUserRealname;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.PageAuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealNameMapper;
import com.heima.user.service.ApUserRealNameService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author shiminghui
 * @date 2024/12/18 20:45
 * @description: TODO
 */
@Service
public class ApUserRealNameServiceImpl implements ApUserRealNameService {

    @Autowired
    private ApUserRealNameMapper apUserRealNameMapper;
    @Autowired
    private IRegisterWmUserClient registerWmUserClient;
    @Autowired
    private ApUserMapper apUserMapper;

    /**
     * 查询实名认证列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(PageAuthDto dto) {

        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();
        LambdaQueryWrapper<ApUserRealname> queryWrapper = Wrappers.lambdaQuery(ApUserRealname.class);
        if (dto.getStatus() != null) {
            queryWrapper.eq(ApUserRealname::getStatus, dto.getStatus());
        }
        if (dto.getId() != null) {
            queryWrapper.eq(ApUserRealname::getId, dto.getId());
        }
        if (dto.getMsg() != null && !dto.getMsg().isEmpty()) {
            queryWrapper.eq(ApUserRealname::getReason, dto.getMsg());
        }

        Page<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        Page<ApUserRealname> resultPage = apUserRealNameMapper.selectPage(page, queryWrapper);

        PageResponseResult result = new PageResponseResult();

        result.setData(resultPage.getRecords());
        result.setTotal((int) resultPage.getTotal());
        result.setCurrentPage(dto.getPage());
        result.setSize(dto.getSize());
        return result;
    }

    /**
     * 审核
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult auth(PageAuthDto dto) {

        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 检查是否存在
        ApUserRealname realname = apUserRealNameMapper.selectById(dto.getId());
        if (realname == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        realname.setStatus(dto.getStatus().shortValue());
        realname.setReason(dto.getMsg());
        int i = apUserRealNameMapper.updateById(realname);
        if (i <= 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }

        if (dto.getStatus() == 9) { //审核通过
            ApUser apUser = apUserMapper.selectById(dto.getId());
            ResponseResult register = registerWmUserClient.register(apUser);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
